package lanou3g.myfirstapp36kr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;
import lanou3g.myfirstapp36kr.myvolleysingleton.Colection;
import lanou3g.myfirstapp36kr.R;

/**
 * Created by dllo on 16/8/16.
 */
public class MyColectionAdapter extends BaseAdapter {
    private List<Colection> data;
    private Context context;
    private LayoutInflater inflater;
    private SimpleDateFormat dateFormat;
    private Date date;

    public MyColectionAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        inflater = LayoutInflater.from(context);
        dateFormat = new SimpleDateFormat("yyyy:MM:dd: HH:mm:ss");

    }

    public void setData(List<Colection> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size() ;
    }

    @Override
    public Object getItem(int i) {

        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.item_colection,null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Picasso.with(context).load(data.get(i).getFeatureImg()).into(viewHolder.imageViewPic);
        viewHolder.textViewTitle.setText(data.get(i).getTitle());
        viewHolder.textViewName.setText(data.get(i).getName());
        date = new Date(data.get(i).getTime());
        viewHolder.textViewTime.setText(dateFormat.format(date));
        viewHolder.textViewType.setText(data.get(i).getColumnName());
        return view;
    }
    private class ViewHolder{
        private ImageView imageViewPic;
        private TextView textViewTitle;
        private TextView textViewName;
        private TextView textViewTime;
        private TextView textViewType;

        public ViewHolder(View view) {
            imageViewPic = (ImageView) view.findViewById(R.id.iv_item_f_news_lv_picture);
            textViewTitle = (TextView) view.findViewById(R.id.tv_item_f_news_lv_title);
            textViewName = (TextView) view.findViewById(R.id.tv_item_f_news_lv_author);
            textViewTime = (TextView) view.findViewById(R.id.tv_item_f_news_lv_data);
            textViewType = (TextView) view.findViewById(R.id.tv_item_f_news_lv_type);
        }
    }
}
